package com.yeren.servlet.tarena.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ModifyEmpServlet extends HttpServlet {
	/*
	 * 使用请求中的name,contact两组参数值 分别使用getParameter和getParameterValues方法获取
	 */
	public void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 解决中文乱码问题
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		// 1.获取请求参数值
		int id=Integer.parseInt(req.getParameter("id"));
		String name = req.getParameter("name");
		double salary = Double.parseDouble(req.getParameter("salary"));
		int age = Integer.parseInt(req.getParameter("age"));
		// 2.数据库操作
		Connection conn = null;
		PreparedStatement stat = null;
		try {
			Class.forName("oracle.jdbc.OracleDriver");
			conn = DriverManager
					.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
							"scott", "tiger");
			stat = conn.prepareStatement("update liu_temp set name=?,salary=?,age=? where id=?");
			stat.setString(1, name);
			stat.setDouble(2, salary);
			stat.setInt(3, age);
			stat.setInt(4, id);
			stat.executeUpdate();
			resp.sendRedirect("list");
		} catch (Exception e) {
			e.printStackTrace();
			out.print("系统出现问题，稍后重试<br/><p><a href='list.do'>员工信息列表</a></p>");
		} finally {         
			if (stat != null) {
				try {
					stat.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

	}
}
